package com.jn.erp.web.api.interaction.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jn.erp.common.core.page.TableDataInfo;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.security.controller.BaseWebAuthController;
import com.jn.erp.web.api.course.entity.TCourseOrder;
import com.jn.erp.web.api.course.entity.dto.TCourseOrderDto;
import com.jn.erp.web.api.course.service.TCourseOrderService;
import com.jn.erp.web.api.interaction.entity.TEvaluate;
import com.jn.erp.web.api.interaction.entity.vo.TEvaluateVo;
import com.jn.erp.web.api.interaction.service.TEvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wyp
 * @since 2024-09-20
 */
@RestController
@RequestMapping("/tEvaluate")
public class TEvaluateController extends BaseWebAuthController {

    @Autowired
    private TEvaluateService tEvaluateService;

    @Autowired
   private TCourseOrderService tCourseOrderService;

    /**
     统计数
     */
    @PostMapping("count")
    public AjaxResult count(@RequestBody TEvaluateVo tEvaluateVo){
        //机构id
        tEvaluateVo.setOrgId(Long.parseLong(loginDeptAuth()) );
        //当前登录人id
        tEvaluateVo.setCreateUser(String.valueOf(getUserId()) );
        Map map = tEvaluateService.count(tEvaluateVo);
        return AjaxResult.success(map);
    }

    /**
     评论列表
     */
    @GetMapping("evaluateList")
    public TableDataInfo evaluateList(TEvaluateVo tEvaluateVo){
        //机构id
        tEvaluateVo.setOrgId(Long.parseLong(loginDeptAuth()) );
        //当前登录人id
        tEvaluateVo.setCreateUser(String.valueOf(getUserId()));

        startPage();
        List<TEvaluate> collect = tEvaluateService.evaluateUnifyList(tEvaluateVo);
//        List<TEvaluate> list =  tEvaluateService.list(new QueryWrapper<TEvaluate>().eq("order_no", tEvaluateVo.getOrderNo()).eq("is_delete", "0").eq("state", tEvaluateVo.getState()).eq("type",1));
//        List<TEvaluate> collect = list.stream().distinct().collect(Collectors.toList());
//        List<TCourseOrder> list1 = tCourseOrderService.list(new QueryWrapper<TCourseOrder>().eq("order_no", tEvaluateVo.getOrderNo()).eq("is_delete", "0").eq("org_id", Long.parseLong(loginDeptAuth())));
//        for (TCourseOrder courseOrder : list1) {
//            for (TEvaluate tEvaluate : collect) {
//                if (courseOrder.getTeacher() == getUserId()) {
//                    tEvaluate.setCode("0");
//                } else {
//                    tEvaluate.setCode("1");
//                }
//            }
//        }
        return getDataTable(collect);
    }


    /**
     评论详情
     */
    @PostMapping("evaluateGetById")
    public AjaxResult evaluateGetById(@RequestBody TEvaluateVo tEvaluateVo){
        //机构id
        tEvaluateVo.setOrgId(Long.parseLong(loginDeptAuth()) );
        //当前登录人id
        tEvaluateVo.setCreateUser(String.valueOf(getUserId()) );
        TEvaluate tEvaluate = tEvaluateService.evaluateGetById(tEvaluateVo);
        return AjaxResult.success(tEvaluate);
    }


    /**
     评论发布
     */
    @PostMapping("updateEvaluate")
    public AjaxResult updateEvaluate(@RequestBody TEvaluateVo tEvaluateVo){
        //机构id
        tEvaluateVo.setOrgId(Long.parseLong(loginDeptAuth()) );
        //当前登录人id
        tEvaluateVo.setUpdateUser(String.valueOf(getUserId()) );
        tEvaluateVo.setUpdateDate(LocalDateTime.now() );
       String code = tEvaluateService.updateEvaluate(tEvaluateVo);
        return AjaxResult.success(code);
    }

    /**
     学员评价列表
     */
    @GetMapping("evaluateStudentList")
    public TableDataInfo evaluateStudentList(TEvaluateVo tEvaluateVo){
        startPage();
        //机构id
        tEvaluateVo.setOrgId(Long.parseLong(loginDeptAuth()) );
        //当前登录人id
        tEvaluateVo.setCreateUser(String.valueOf(getUserId()));
        List<TCourseOrderDto> code = tEvaluateService.evaluateStudentList(tEvaluateVo);
        return getDataTable(code);
    }

    /**
     评价记录
     */
    @GetMapping("/evaluationRecordsList")
    public TableDataInfo evaluationRecordsList(TEvaluateVo tEvaluateVo){
        startPage();
        //机构id
        tEvaluateVo.setOrgId(Long.parseLong(loginDeptAuth()) );
        //当前登录人id
        tEvaluateVo.setCreateUser(String.valueOf(getUserId()) );
        List<TCourseOrderDto> list = tEvaluateService.evaluationRecordsList(tEvaluateVo);
        return getDataTable(list);

    }


    /**
     老师评价列表
     */
    @GetMapping("evaluateTeacherList")
    public TableDataInfo evaluateTeacherList(TEvaluateVo tEvaluateVo){
        startPage();
        //机构id
        tEvaluateVo.setOrgId(Long.parseLong(loginDeptAuth()) );
        //当前登录人id
        tEvaluateVo.setCreateUser(String.valueOf(getUserId()));
        List<TCourseOrderDto> code = tEvaluateService.evaluateTeacherList(tEvaluateVo);
        return getDataTable(code);
    }

}

